home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / HighLevelEventLib / HighLevelEventLib.c next >
Encoding:
C/C++ Source or Header  |  1993-11-15  |  4.2 KB  |  187 lines  |  [TEXT/KAHL]

  1. /* Handle high-level events.
  2.  
  3.     93/11/15 aih
  4.     - for compatability with prior system software, added functions for
  5.     opening and printing a single file pointer without using the Apple event
  6.     manager
  7.     
  8.     93/10/20 aih
  9.     - added functions to open or print a list of files
  10.     
  11.     93/10/17 aih
  12.     - made handlers more generic by allowing application to install its own
  13.     handlers
  14.     
  15.     93/03/19 AIH
  16.     - Moved pre-system 7.0 file open/print to ApplicationLib.c
  17.     
  18.     93/03/10 Ari Halberstadt (AIH)
  19.     - Created */
  20.  
  21. #include <AppleEvents.h>
  22. #include "EventLib.h"
  23. #include "FileLib.h"
  24. #include "HighLevelEventLib.h"
  25. #include "MacLib.h"
  26.  
  27. typedef struct {
  28.     void (*handler)(...);
  29.     void *data;
  30. } HighLevelType;
  31.  
  32. static HighLevelType handlers[HL_LAST];
  33.  
  34. void HLEventInstall(HighLevelKind kind, void (*handler)(...),
  35.     void *data)
  36. {
  37.     require(HL_FIRST <= kind && kind < HL_LAST);
  38.     handlers[kind].handler = handler;
  39.     handlers[kind].data = data;
  40. }
  41.  
  42. void HLEventOpen(AEDescList *list, long n)
  43. {
  44.     if (handlers[HL_OPEN].handler)
  45.         handlers[HL_OPEN].handler(list, n, handlers[HL_OPEN].data);
  46. }
  47.  
  48. void HLEventOpenOne(FileType *fp)
  49. {
  50.     if (handlers[HL_OPEN_ONE].handler)
  51.         handlers[HL_OPEN_ONE].handler(fp, handlers[HL_OPEN_ONE].data);
  52. }
  53.  
  54. void HLEventPrint(AEDescList *list, long n)
  55. {
  56.     if (handlers[HL_PRINT].handler)
  57.         handlers[HL_PRINT].handler(list, n, handlers[HL_PRINT].data);
  58. }
  59.  
  60. void HLEventPrintOne(FileType *fp)
  61. {
  62.     if (handlers[HL_PRINT_ONE].handler)
  63.         handlers[HL_PRINT_ONE].handler(fp, handlers[HL_PRINT_ONE].data);
  64. }
  65.  
  66. void HLEventOpenApplication(void)
  67. {
  68.     if (handlers[HL_OAPP].handler)
  69.         handlers[HL_OAPP].handler(handlers[HL_OAPP].data);
  70. }
  71.  
  72. void HLEventQuit(void)
  73. {
  74.     if (handlers[HL_QUIT].handler)
  75.         handlers[HL_QUIT].handler(handlers[HL_QUIT].data);
  76.     EventLoopExit(true); /* handler can raise an exception to prevent quitting */
  77. }
  78.  
  79. /* count number of files in list of files */
  80. long AECountFiles(AEDescList *list)
  81. {
  82.     long n;
  83.     
  84.     FailOSErr(AECountItems(list, &n));
  85.     return(n);
  86. }
  87.  
  88. /* get n'th file in list of files */
  89. void AEGetNthFile(AEDescList *list, long n, FileType *fp)
  90. {
  91.     AEKeyword keywd;
  92.     Size actsize;
  93.     DescType type;
  94.     FSSpec fspec;
  95.  
  96.     FailOSErr(AEGetNthPtr(list, n, typeFSS, &keywd, &type,
  97.                                     (Ptr) &fspec, sizeof(fspec), &actsize));
  98.     FileSetFSSpec(fp, &fspec);
  99. }
  100.  
  101. void AEGotRequiredParameters(AppleEvent *event)
  102. {
  103.     DescType type;
  104.     Size actsize;
  105.     OSErr err = noErr;
  106.     
  107.     err = AEGetAttributePtr(event, keyMissedKeywordAttr, typeWildCard,
  108.                 &type, NULL, 0, &actsize);
  109.     if (! err)
  110.         err = errAEEventNotHandled;
  111.     else if (err == errAEDescNotFound)
  112.         err = noErr;
  113.     FailOSErr(err);
  114. }
  115.  
  116. static OSErr DoAEOpenOrPrint(AppleEvent *event,
  117.     void (*action)(AEDescList *list, long n))
  118. {
  119.     volatile AEDescList list;
  120.     volatile Boolean gotlist = false;
  121.     OSErr err = noErr;
  122.  
  123.     TRY {
  124.         FailOSErr(AEGetParamDesc(event, keyDirectObject, typeAEList, &list)); gotlist = true;
  125.         AEGotRequiredParameters(event);
  126.         action(&list, AECountFiles(&list));
  127.     } CLEANUP {
  128.         if (gotlist) AEDisposeDesc(&list);
  129.     } CATCH {
  130.         err = FailReason();
  131.         NOPROPAGATE;        
  132.     } ENDTRY;
  133.     return(err);
  134. }
  135.  
  136. static pascal OSErr HandleAEOpen(AppleEvent *event,
  137.     AppleEvent *reply, long refcon)
  138. {
  139.     return(DoAEOpenOrPrint(event, HLEventOpen));
  140. }
  141.  
  142. static pascal OSErr HandleAEPrint(AppleEvent *event,
  143.     AppleEvent *reply, long refcon)
  144. {
  145.     return(DoAEOpenOrPrint(event, HLEventPrint));
  146. }
  147.  
  148. static pascal OSErr HandleAEQuit(AppleEvent *event,
  149.     AppleEvent *reply, long refcon)
  150. {
  151.     OSErr err = noErr;
  152.     
  153.     TRY {
  154.         AEGotRequiredParameters(event);
  155.         HLEventQuit();
  156.     } CATCH {
  157.         err = FailReason();
  158.         NOPROPAGATE;
  159.     } ENDTRY;
  160.     return(err);
  161. }
  162.  
  163. static pascal OSErr HandleAEOpenApplication(AppleEvent *event,
  164.     AppleEvent *reply, long refcon)
  165. {
  166.     OSErr err = noErr;
  167.     
  168.     TRY {
  169.         AEGotRequiredParameters(event);
  170.         HLEventOpenApplication();
  171.     } CATCH {
  172.         err = FailReason();
  173.         NOPROPAGATE;
  174.     } ENDTRY;
  175.     return(err);
  176. }
  177.  
  178. void HLEventInit(void)
  179. {
  180.     if (MacHasAppleEvents()) {
  181.         FailOSErr(AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, HandleAEOpenApplication, 0, false));
  182.         FailOSErr(AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, HandleAEOpen, 0, false));
  183.         FailOSErr(AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, HandleAEPrint, 0, false));
  184.         FailOSErr(AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, HandleAEQuit, 0, false));
  185.     }
  186. }
  187.